package com.liuwei.spring.cloud.common.utils;

import org.apache.commons.lang.StringUtils;

import java.math.BigInteger;
import java.util.Date;

public class Radix {

    public static void main(String[] args) {
        String hex = increaseRadixString("1JLV",23,32);
        System.out.println(hex);
        String value = StringUtils.leftPad(hex,8,"X");
        System.out.println(value);
        String prefix = "T";
        System.out.println(prefix+DateHelper.format(new Date(),DateHelper.NOSPLIT_YMD)+value);
    }

    /**
     * 将 10 转换为任意进制字符串
     * @param radix 任意进制(2-36进制)
     * @param number 10 进制整数
     * @return 字符串
     */
    public static String toRadixString(long number,int radix){
        return Long.toString(number,radix).toUpperCase();
    }

    public static String toBinaryString(long number){
        return Long.toBinaryString(number).toUpperCase();
    }

    public static String toOctalString(long number){
        return Long.toOctalString(number).toUpperCase();
    }

    public static String toHexString(long number){
        return Long.toHexString(number).toUpperCase();
    }

    public static String to32HexString(long number) {
        return Long.toString(number, 32).toUpperCase();
    }

    public static String increaseRadixString(String hexString,long step,int radix) {
        long value = fromRadixString(hexString,radix);
        return toRadixString(value+step,radix);
    }

    public static String minusRadixString(String hexString,long step,int radix) {
        long value = fromRadixString(hexString,radix);
        return toRadixString(value-step,radix);
    }

    /**
     * 将任意进制字符串转换为10进制证书
     * @param radix 任意进制(2-36进制)
     * @param hexString 字符串
     * @return 10 进制整数
     */
    public static long fromRadixString(String hexString,int radix) {
        return Long.parseLong(hexString, radix);
    }

    public static long fromBinaryString(String hexString) {
        return Long.parseLong(hexString, 2);
    }

    public static long fromOctalString(String hexString) {
        return Long.parseLong(hexString, 8);
    }

    public static long fromHexString(String hexString) {
        return Long.parseLong(hexString, 16);
    }

    public static long from32HexString(String hexString) {
        return Long.parseLong(hexString, 32);
    }

}
